<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">商品管理</div></el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><div class="div2">商品品牌</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 -->
    <!-- 添加客户等级 -->
    <div class="content">
      <p class="header">添加品牌</p>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="产品品牌">
          <el-input
            maxlength="125"
            v-model="formInline.name"
            placeholder="请输入添加品牌"
          ></el-input>
        </el-form-item>
        <el-form-item label="英文名">
          <el-input
            maxlength="125"
            v-model="formInline.ename"
            placeholder="请输入添加品牌"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
            v-if="parArray.indexOf('addBrand') != -1"
            >添加品牌</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加客户等级 -->
    <!-- 管理客户等级 -->
    <div class="manage">
      <p class="header">管理品牌</p>
      <div class="table">
        <el-table :data="tableData.list">
          <el-table-column type="index" label="序号"></el-table-column>

          <el-table-column scope="scope" label="品牌名称">
            <template slot-scope="scope">
              <el-input maxlength="125" v-model="scope.row.name"></el-input>
            </template>
          </el-table-column>

          <el-table-column scope="scope" label="品牌英文名">
            <template slot-scope="scope">
              <el-input maxlength="125" v-model="scope.row.fenName"></el-input>
            </template>
          </el-table-column>

          <el-table-column
            label="更新时间"
            prop="createdTime"
          ></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="optain">
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="保存"
                  placement="top-start"
                >
                  <p
                    @click="preservation(scope.row)"
                    v-if="parArray.indexOf('brandbc') != -1"
                  >
                    <span>保存</span>
                    <!-- <img
                      style="width:18px;"
                      src="../../../assets/baocun.png"
                      alt=""
                    /> -->
                  </p>
                </el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="删除"
                  placement="top-start"
                >
                  <p
                    @click="delect(scope.row, scope.$index)"
                    v-if="parArray.indexOf('brandDelect') != -1"
                  >
                    <span>删除</span>
                    <!-- <img src="../../../assets/delect.png" alt="" /> -->
                  </p>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页start -->
        <div class="page" v-if="tableData.totalNum >= 7">
          <div class="block">
            <span class="demonstration">共{{ tableData.totalPage }}页</span>
            <span class="demonstration">总共{{ tableData.totalNum }}条</span>
            <el-pagination
              background=""
              @current-change="handleCurrentChange"
              :current-page.sync="currentPage1"
              :page-size="1"
              layout="prev, pager, next"
              prev-text="上一页"
              next-text="下一页"
              :total="tableData.totalPage"
            >
            </el-pagination>
          </div>
        </div>
        <!-- 分页end -->
      </div>
    </div>
    <!-- 管理客户等级 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: {
        name: "",
        ename: "",
      },
      tableData: [],
      currentPage1: 1,
      limits: "",
      parArray: [],
    };
  },
  created() {
    this.brandlist("1");
    // 权限设定判断
    this.limits = JSON.parse(sessionStorage.getItem("limit"));
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === "brandCode"
    );
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code);
    });
    // 权限设定判断
  },
  activated() {
    //  this.brandlist("1");
  },
  methods: {
    /***品牌列表查询***/
    brandlist(pageNo) {
      this.axios
        .post("/order/comm/commtype/selectCommTypeBycode", {
          name: "",
          pageNo: pageNo,
          pageSize: 8,
          id: "10000",
        })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            this.tableData = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    /***品牌列表查询***/
    handleCurrentChange(val) {
      this.brandlist(`${val}`);
    },
    /**添加分类**/
    onSubmit() {
      if (this.formInline.name == "") {
        this.$message.error("产品分类不能为空");
      } else if (this.formInline.ename == "") {
        this.$message.error("英文不能为空");
      } else {
        this.axios
          .post(
            "/order/comm/commtype/addCommTypeBycode",
            {
              createdBy: sessionStorage.getItem("user_id"),
              name: this.formInline.name,
              fenName: this.formInline.ename,
              pcode: "10000",
            },
            {
              headers: {
                isloading: false,
              },
            }
          )
          .then((res) => {
            if (res.code == 0) {
              this.$message.success("添加成功");
              this.formInline.name = "";
              this.formInline.ename = "";
              this.brandlist();
            } else {
              this.$message.error(res.message);
            }
          });
      }
    },
    // 跳转子分类
    /***删除数据对接***/
    delect(row, index) {
      this.$confirm("确定要删除该分类吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .get("/order/comm/commtype/delete?id=" + row.id)
            .then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.tableData.list.splice(index, 1);
                this.brandlist("1");
                this.$message.success("删除成功");
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /***删除数据对接***/

    /***修改数据对接***/
    preservation(row) {
      if (!row.name || !row.fenName) {
        this.$message.warning("不能为空");
        return;
      }
      this.axios
        .post("/order/comm/commtype/updateCommType", {
          id: row.id,
          updatedBy: sessionStorage.getItem("user_id"),
          name: row.name,
          fenName: row.fenName,
        })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            this.$message.success("修改成功");
            this.brandlist("1");
          } else {
            this.$message.error(res.message);
          }
        });
    },
    /***修改数据对接***/
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.content {
  padding: 10px;
  background: #fff;
  .el-button {
    padding: 6px 10px;
  }
}
/deep/.el-input {
  width: 200px;
}
.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.demo-form-inline {
  margin-top: 10px;
}
/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}
.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
}
/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}
/deep/.cell {
  text-align: center;
}
/deep/.el-table__row .cell {
}
.optain {
  width: 100%;
  text-align: center;
}
.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}
.optain {
  p {
    display: inline-block;
    margin-left: 20px;
    img {
      width: 20px;
      height: 20px;
    }
  }
  p:hover {
    cursor: pointer;
  }
}
.page {
  margin: 20px 0;
  text-align: right;
  width: 100%;
  span {
    font-size: 15px;
    color: #888;
    display: inline-block;
    margin-left: 10px;
    margin-top: -2px;
    vertical-align: middle;
  }
  .el-pagination {
    display: inline-block;
    vertical-align: middle;
  }
}
</style>
